
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded'
let baseURL = process.env.VUE_APP_API

axios.defaults.baseURL = baseURL
axios.defaults.withCredentials = true
axios.defaults.withCredentials = false
export default function request(url, type = 'GET', data = {}, config = {
    showProgress: true,
    uploadModel: false,
    isToken: true,
    configurl: undefined
}) {
    return new Promise((resolve, reject) => {
        if (config.isToken == undefined) config.isToken = true

        if (config.configurl != undefined) {
            axios.defaults.baseURL = config.configurl
        } else {
            axios.defaults.baseURL = baseURL
        }

        let option = {
            url,
            method: type
        }
        if (type.toLowerCase() === 'get') {
            option.params = data
        } else {
            option.data = data
        }
        // if (config.isToken) {
        //     if (localStorage.getItem('user') == undefined) return
        //     option.headers = {
        //         'Authorization': `Bearer ${JSON.parse(localStorage.getItem('user')).accessToken}`
        //     }
        //     // localStorage.getItem('user')
        // }

        axios(option).then(res => { //axios的res被封装了一次 所以真实数据在res.data中
            console.log(res)
            if (res.data.ok) {
                resolve(res.data)
            } else {
                // if (res.data.code == 401) {
                //     // console.log()
                //     // window.location.href = `http://${window.location.host}/login?redirect=${window.location.pathname + window.location.search}`
                // }
                reject(res.data)
            }
        }).catch(err => {
            reject({
                msg: '网络异常:' + err
            })
        })
    })
}


// request('/auth/login', 'POST', {username: 'hunger', password: '123456'})
//   .then(data=>{
//     console.log(data)
//   })
